#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include "esp_log.h"
#include "Drivers/nvs_drv.h"
#include "App/sn_handle.h"
#include "esp_mac.h"
#include "driver/uart.h"
#include "Drivers/uart_drv.h"
#include "Drivers/wifi_drv.h"
#include "App/ota_task.h"
#include "tools.h"

#define TAG   "sn_handle"
#define SKIP_SN_CHECK     0                     //跳过sn检测

static uint8_t g_MAC[MAC_LEN];                  //mac地址
static uint8_t g_SN[SN_LEN];                    //sn

static uint8_t g_MAC_Str[MAC_LEN * 2 + 1] ;     //mac地址字符串
static uint8_t g_SN_Str[SN_LEN * 2 + 1] ;       //sn字符串

static bool g_SnIsExist = false;                //sn是否存在

EventGroupHandle_t SnEventGroup = NULL;         //sn事件组

/**
 * @brief SN处理任务函数
 *
 * 该任务负责处理设备序列号(SN)相关操作:
 * 1. 获取设备MAC地址并转换为字符串格式
 * 2. 检查NVS中是否存在SN
 * 3. 如果SN存在,打印设备信息(SN、MAC、版本号)并退出
 * 4. 如果SN不存在,创建事件组并循环发送SN请求
 *
 * @param param 任务参数(未使用)
 * @note 当SKIP_SN_CHECK宏定义时,将使用默认SN
 */
void SnHandleTask(void *param)
{
    ESP_LOGI(TAG, "sn_handle_task start");

    esp_efuse_mac_get_default(g_MAC);           //获取mac地址

    for (int i = 0; i < MAC_LEN; i++) 
        sprintf((char*)g_MAC_Str + (i*2),"%02X", (char)g_MAC[i]); 

#if !SKIP_SN_CHECK

    SnEventGroup = xEventGroupCreate();             //创建sn事件组

    EventBits_t eBits = 0;

    if(read_nvs_space(NVS_SN_NAMESPACE, NVS_SN_KEY,(char*)g_SN_Str, SN_LEN_STR - 1) == ESP_OK)
    {
        xEventGroupSetBits(SnEventGroup, SN_EXIST_BIT);             //sn存在，打印信息并退出
        g_SnIsExist = true;

        printf("=======================================\n");
        printf("= S N:%s                  =\n", g_SN_Str);
        // printf("---------------------------------------\n");
        printf("= MAC:%s                    =\n",g_MAC_Str);
        // printf("---------------------------------------\n");
        printf("= VERSION:%s                       =\n", LOCAL_VERSION); 
        printf("---------------------------------------\n");
        printf("=  ____  __  __ _  __                 =\n");
        printf("= |  _ \\|  \\/  | |/ /                 =\n");
        printf("= | |_) | |\\/| | ' /                  =\n");
        printf("= |  __/| |  | | . \\                  =\n");
        printf("= |_|   |_|  |_|_|\\_\\                 =\n");
        printf("=======================================\n");

        sn_to_hex((char*)g_SN_Str,(char*) g_SN);
        vTaskDelete(NULL);
    }
    else   xEventGroupSetBits(SnEventGroup, SN_REQUEST_BIT);            //sn不存在，发送sn请求

    
    while (1)
    {

        eBits = xEventGroupWaitBits(SnEventGroup, SN_REQUEST_BIT, false, false, portMAX_DELAY);     

        if(eBits & SN_REQUEST_BIT)
        {
            uart_write_bytes(UART_CONSOLE_NUM, SN_REQUEST, sizeof(SN_REQUEST));             //发送sn请求
        }
        
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
#else
    strcpy((char*)g_SN_Str,"43002400000001");               //默认sn
    sn_to_hex((char*)g_SN_Str, (char*)g_SN);

    vTaskDelete(NULL);
#endif
}
/**
 * @brief 获取sn接口函数
 */
uint8_t *GetSN(void)
{
    return g_SN;
}

/**
 * @brief 获取mac接口函数
 */
uint8_t *GetMAC(void)
{
    return g_MAC;
}

/**
 * @brief 获取sn字符串接口函数
 */
uint8_t *GetSNStr(void)
{
    return g_SN_Str;
}

/**
 * @brief 获取mac字符串接口函数
 */
uint8_t *GetMACStr(void)
{
    return g_MAC_Str;
}

/**
 * @brief 获取sn状态接口函数
*/
bool GetSNStatus(void)
{
    return g_SnIsExist;
}